﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYRevitCode.FormUtility;

namespace YArchitech.HVAC
{
	public partial class BatchConnTerminalForm : HYBaseForm
	{
		public bool BeAutoCancel { get; set; }

		public BatchConnTerminalForm.ConnStyle BatchConnStyle { get; set; }

		public int AlignType { get; set; }

		public BatchConnTerminalForm()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			List<KeyValuePair<System.Windows.Forms.Control, bool>> inputControlGroup = new List<KeyValuePair<System.Windows.Forms.Control, bool>>();
			this.m_thisManger = new ControlInfoManager(this, inputControlGroup, "{F7818EB1-274F-4BB6-B2F9-2D355FA89C70}", null);
			this.userSetting = new HVACUserSetting();
		}

		private void BatchConnTerminalForm_Load(object sender, EventArgs e)
		{
			if (this.m_thisManger != null && !this.m_thisManger.SetControlInfoToForm())
			{
				base.Location = new System.Drawing.Point(230, 170);
			}
			base.Icon = Resource.YArchitechIcon;
			this.InitUI();
		}

		private void InitUI()
		{
			this.InitialButton();
			this.InitialRadio();
		}

		private void InitialRadio()
		{
			if (this.userSetting.GetTerminalConnectionType() == "0")
			{
				this.rboDirectConnection.Checked = true;
			}
			else
			{
				this.rboFlexDuctConnection.Checked = true;
			}
			string terminalAlignType = this.userSetting.GetTerminalAlignType();
			if (terminalAlignType == "0")
			{
				this.rdoTopAlign.Checked = true;
				this.AlignType = 0;
				return;
			}
			if (terminalAlignType == "2")
			{
				this.rdoBottomAlign.Checked = true;
				this.AlignType = 2;
				return;
			}
			this.rdoMiddleAlign.Checked = true;
			this.AlignType = 1;
		}

		private void InitialButton()
		{
			string text = this.userSetting.GetBatchConnTerminalStyle();
			if (string.IsNullOrEmpty(text))
			{
				text = "Fitting";
			}
			if (text == "Fitting")
			{
				this.SelectButton(this.btnFitting);
				return;
			}
			if (text == "OnDuct")
			{
				this.SelectButton(this.btnOnDuct);
				return;
			}
			if (!(text == "Intubation"))
			{
				return;
			}
			this.SelectButton(this.btnIntubation);
		}

		private void btnOnDuct_Click(object sender, EventArgs e)
		{
			System.Windows.Forms.Control control = sender as Button;
			System.Drawing.Color backColor = this.BackColor;
			System.Drawing.Color lightSkyBlue = System.Drawing.Color.LightSkyBlue;
			string name = control.Name;
			if (!(name == "btnFitting"))
			{
				if (!(name == "btnOnDuct"))
				{
					if (name == "btnIntubation")
					{
						this.BatchConnStyle = BatchConnTerminalForm.ConnStyle.Intubation;
						this.btnIntubation.BackColor = lightSkyBlue;
						this.btnOnDuct.BackColor = backColor;
						this.btnFitting.BackColor = backColor;
					}
				}
				else
				{
					this.BatchConnStyle = BatchConnTerminalForm.ConnStyle.OnDuct;
					this.btnFitting.BackColor = (this.btnIntubation.BackColor = backColor);
					this.btnOnDuct.BackColor = lightSkyBlue;
				}
			}
			else
			{
				this.BatchConnStyle = BatchConnTerminalForm.ConnStyle.Fitting;
				this.btnFitting.BackColor = lightSkyBlue;
				this.btnOnDuct.BackColor = (this.btnIntubation.BackColor = backColor);
			}
			this.ChangeOperateState();
		}

		private void BatchConnTerminalForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.m_thisManger != null)
			{
				this.m_thisManger.GetControlInfoToXml();
			}
			this.userSetting.WriteBatchConnTerminalStyle(this.BatchConnStyle.ToString());
			this.userSetting.WriteTerminalConnectionType(this.TerminalConnType.ToString());
			this.userSetting.WriteTerminalAlignType(this.AlignType.ToString());
		}

		private void SelectButton(Button button)
		{
			button.PerformClick();
			button.Select();
			button.Focus();
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void BatchConnTerminalForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			BatchConnTerminalForm.SendMessage(HYDuctConnectAirrTerminalByRectangle.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			BatchConnTerminalForm.keybd_event((byte)bVk, 0, dwFlags, 0);
			BatchConnTerminalForm.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void BatchConnTerminalForm_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private void BatchConnTerminalForm_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(Cursor.Position))
			{
				int wMsg = 7;
				BatchConnTerminalForm.SendMessage(HYDuctConnectAirrTerminalByRectangle.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		private void ChangeOperateState()
		{
			Cursor.Position = new System.Drawing.Point(Cursor.Position.X, base.Bottom + 1);
			this.SendSetfocusMessage();
			this.SimulationPressESC();
			this.BeAutoCancel = true;
		}

		private void SimulationPressESC()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			BatchConnTerminalForm.keybd_event((byte)bVk, 0, dwFlags, 0);
			BatchConnTerminalForm.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void SendSetfocusMessage()
		{
			int wMsg = 7;
			BatchConnTerminalForm.SendMessage(HYDuctConnectAirrTerminalByRectangle.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
		}

		private void ChkConn_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rboFlexDuctConnection.Checked)
			{
				this.TerminalConnType = 1;
				return;
			}
			this.TerminalConnType = 0;
		}

		private void ChkAlign_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoTopAlign.Checked)
			{
				this.AlignType = 0;
				return;
			}
			if (this.rdoMiddleAlign.Checked)
			{
				this.AlignType = 1;
				return;
			}
			if (this.rdoBottomAlign.Checked)
			{
				this.AlignType = 2;
			}
		}

		public int TerminalConnType;

		private HVACUserSetting userSetting;

		private ControlInfoManager m_thisManger;

		public enum ConnStyle
		{
			Fitting,
			OnDuct,
			Intubation
		}
	}
}
